package com.hosever.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hosever.core.AbstractController;
import com.hosever.model.DemoModel;
import com.hosever.service.DemoService;

@Controller
@RequestMapping("/demo")
public class DemoController extends AbstractController<DemoModel,DemoService>{
	
	
	@RequestMapping(value = "/login/{user}", method = RequestMethod.GET)  
    public ModelAndView myMethod(HttpServletRequest request,HttpServletResponse response,   
            @PathVariable("user") String user, ModelMap modelMap,RedirectAttributes redirectAttributes) throws Exception {  
        modelMap.put("loginUser", user);  
        
        //带参重定向
        //redirectAttributes.addFlashAttribute("message", "操作成功");  
        return new ModelAndView("login/hello", modelMap);  
    }  
	
    @RequestMapping(value = "/welcome", method = RequestMethod.GET)  
    public String registPost() {  
        return "welcome";  
    }
    
    @RequestMapping(value = "/test", method = RequestMethod.GET)  
    public String test(Model model){
    	model.addAttribute("test","hello");
    	return getRelativePath("/test");
    }
    @RequestMapping(value = "/test", method = RequestMethod.POST)  
    public String test(Model model,@ModelAttribute("dm") DemoModel dm){
    	model.addAttribute("test","hello");
    	System.out.println(dm.getUsername());
    	System.out.println(dm.getPassword());
    	return getRelativePath("/test");
    }
    
    /** 
     * ajax测试 
    * http://127.0.0.1/mvc/order/ajax 
     */  
    @RequestMapping("/ajax")  
    @ResponseBody  
    public Object ajax(HttpServletRequest request){  
        List<String> list=new ArrayList<String>();  
        list.add("电视");  
        list.add("洗衣机");  
        list.add("冰箱");  
        list.add("电脑");  
        list.add("汽车");  
        list.add("空调");  
        list.add("自行车");  
        list.add("饮水机");  
        list.add("热水器");  
        return list;  
    }  
}
